package com.permission_server.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.permission_server.entity.SysRoleEntity;
import com.permission_server.entity.SysRolePermissionEntity;
import com.permission_server.entity.SysUserEntity;
import com.permission_server.service.SysRolePermissionService;
import com.permission_server.service.SysRoleService;
import com.permission_server.util.responseUtil.ResponseEnum;
import com.permission_server.util.responseUtil.ResponseUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理接口")
public class RoleController {


    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysRolePermissionService sysRolePermissionService;

    @GetMapping("/pullAllRole")
    public ResponseUtil pullAllRole() {
        return ResponseUtil.setE(ResponseEnum.OK).setData(sysRoleService.list());
    }

    @PostMapping("/updateRole")
    public ResponseUtil updateRole(@RequestBody SysRoleEntity role) {
        if (ObjectUtil.isEmpty(role)) return ResponseUtil.setE(ResponseEnum.PARAMS_ERROR);
        SysUserEntity userEntity = LoginController.getCurrentLoginUserInfo();
        if (StrUtil.isEmpty(role.getId())) {
            // 新增
            role.setCreateBy(userEntity.getUsername());
            role.setCreateTime(new Date());
            role.setUpdateBy(userEntity.getUsername());
            role.setUpdateTime(new Date());
        } else {
            // 更新
            role.setUpdateBy(userEntity.getUsername());
            role.setUpdateTime(new Date());
        }
        boolean saveOrUpdate = sysRoleService.saveOrUpdate(role);
        if (saveOrUpdate)
            return ResponseUtil.setE(ResponseEnum.OK).setData(role).setMsg("角色【" + role.getRoleName() + "】更新成功");
        return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("角色【" + role.getRoleName() + "】更新失败");
    }

    @PostMapping("/delRoles")
    public ResponseUtil delRoles(@RequestBody String[] idList) {
        if (ObjectUtil.isEmpty(idList)) return ResponseUtil.setE(ResponseEnum.PARAMS_ERROR);
        boolean remove = sysRoleService.removeByIds(Arrays.asList(idList));
        if (remove) return ResponseUtil.setE(ResponseEnum.OK).setMsg("【" + idList.length + "】个角色删除成功");
        return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("【" + idList.length + "】个角色删除失败");
    }

    @GetMapping("/pullAllRolePermission")
    public ResponseUtil pullAllRolePermission() {
        return ResponseUtil.setE(ResponseEnum.OK).setData(sysRolePermissionService.list());
    }

    @Transactional
    @PostMapping("/updateRolePermission")
    public ResponseUtil updateRolePermission(@RequestBody List<SysRolePermissionEntity> rolePermissionEntities) {
        if (ObjectUtil.isEmpty(rolePermissionEntities)) return ResponseUtil.setE(ResponseEnum.PARAMS_ERROR);
        String roleId = rolePermissionEntities.get(0).getRoleId();
        LambdaQueryWrapper<SysRolePermissionEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRolePermissionEntity::getRoleId, roleId);
        try {
            sysRolePermissionService.remove(lambdaQueryWrapper);
            if (rolePermissionEntities.size() == 1) {
                return ResponseUtil.setE(ResponseEnum.OK).setData(rolePermissionEntities).setMsg("授权成功");
            }
            sysRolePermissionService.saveBatch(rolePermissionEntities);
            return ResponseUtil.setE(ResponseEnum.OK).setData(rolePermissionEntities).setMsg("授权成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("授权失败");
        }
    }
}
